package com.wll.web.controller;

import java.util.List;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;
import com.wll.web.entity.User;
import com.wll.web.exception.UserNotFoundException;
import com.wll.web.service.UserService;

@RestController
@RequestMapping(value="/user")
public class UserController {
	private UserService userService;
	@Autowired
	public UserController(UserService userService){
		this.userService = userService;
	}
	@RequestMapping(value="/login",method={RequestMethod.POST,RequestMethod.GET} )
	public ModelAndView login(@RequestParam String userName,@RequestParam String passWord){
		ModelAndView mv = new ModelAndView("main.jsp");
		return mv;
	}
	@RequestMapping(method=RequestMethod.GET,produces="application/json")
	public @ResponseBody List<User> users(@RequestParam(value="size",defaultValue="10L") long size){
		return userService.users(size);
	}
	@RequestMapping(value="/{id}",method=RequestMethod.GET)
	public ResponseEntity<?> findById(@PathVariable long id){
		User user = userService.findById(id);
		if(user == null){
			Error error = new Error(404, "not found["+id+"]");
			return new ResponseEntity<Error>(error, HttpStatus.NOT_FOUND);
		}
		return new ResponseEntity<User>(user, HttpStatus.OK);
	}
	public @ResponseBody User findById1(@PathVariable long id){
		RestTemplate rest = new RestTemplate();
//		rest.de
		User user = userService.findById(id);
		if(user == null){throw new UserNotFoundException(id);}
		return user;
	}
	public class Error{
		int code;
		String message;
		public Error(int code, String message) {
			super();
			this.code = code;
			this.message = message;
		}
		public int getCode() {
			return code;
		}
		public void setCode(int code) {
			this.code = code;
		}
		public String getMessage() {
			return message;
		}
		public void setMessage(String message) {
			this.message = message;
		}
		
		
	}
	@ExceptionHandler(UserNotFoundException.class)
	@ResponseStatus(HttpStatus.NOT_FOUND)
	public Error userNotFound(UserNotFoundException e){
		return new Error(404, "user["+e.getId()+"]not found");
	}
	public static void main(String[] args) {
		RestTemplate rest = new RestTemplate();
		try {
			ResponseEntity<String> response = rest.getForEntity("http://172.20.10.32:8080/hexin-msg/msg/asyncSendMsg1", String.class,"");
			System.out.println(response.getStatusCodeValue());
		} catch (RestClientException e) {
			if (e instanceof HttpClientErrorException){
				HttpClientErrorException e1 =(HttpClientErrorException)e;
				e1.getStatusCode();
			}
			throw new RestClientException("");
		}
	}
	@ExceptionHandler(RestClientException.class)
	public void test(RestClientException e){
	}
}
